using System;
using System.Windows.Forms;

namespace MapMaker
{
    public partial class frmSelectZoom : Form
    {
        private int levelOfDetail = 0;
        private bool zoomLoaded = false;

        public frmSelectZoom(int LevelOfDetail)
        {
            InitializeComponent();
            levelOfDetail = LevelOfDetail;
        }

        private void frmSelectZoom_Load(object sender, EventArgs e)
        {
            int zoom = 0;
            CheckBox chk;

            foreach (Control ctrl in Controls)
            {                
                if (ctrl.Tag != null)
                {
                    chk = (CheckBox)ctrl;
                    zoom = int.Parse((string)chk.Tag);

                    if (zoom < levelOfDetail + VirtualEarth.VirtualEarthTileAccess.PREVIEW_STEP)
                    {
                        chk.Enabled = false;
                    }

                    if (zoom >= levelOfDetail && zoom < levelOfDetail + VirtualEarth.VirtualEarthTileAccess.PREVIEW_STEP)
                    {
                        chk.Checked = true;
                    }
                    else
                    {
                        if (Program.TileAccess.ZoomExceptions.Contains(zoom) ||
                            zoom < levelOfDetail)
                        {
                            chk.Checked = false;
                        }
                        else
                        {
                            chk.Checked = true;
                        }
                    }
                }
            }

            zoomLoaded = true;
        }

        private void chkZoom_CheckedChanged(object sender, System.EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            int zoom = int.Parse((string)chk.Tag);

            if(zoomLoaded)
            {
                if (chk.Checked && Program.TileAccess.ZoomExceptions.Contains(zoom))
                {
                    Program.TileAccess.ZoomExceptions.Remove(zoom);
                }
                else if (!chk.Checked && !Program.TileAccess.ZoomExceptions.Contains(zoom))
                {
                    Program.TileAccess.ZoomExceptions.Add(zoom);
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}